package com.beimihu.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class RandomizedSet {
    List<Integer> list;
    HashMap<Integer,Integer> hashMaps;

    Random random;


    public RandomizedSet() {
        list = new ArrayList<Integer>();
        hashMaps = new HashMap<Integer,Integer>();
        random = new Random();

    }

    public boolean insert(int val) {
        if(hashMaps.containsKey(val)){
            return false;
        }
        hashMaps.put(val,val);
        list.add(val);
        return true;
    }

    public boolean remove(int val) {
        if (hashMaps.containsKey(val)){
            hashMaps.remove(val);
            list.remove(val);
            return true;
        }
        else{
            return false;
        }
    }

    public int getRandom() {
        int randomNum = random.nextInt(list.size());
        return list.get(randomNum);
    }
}
